package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerTraceHistoryQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("customerTraceHistory")
@Controller
public class CustomerTraceHistoryController {

	@Autowired
	private ICustomerTraceHistoryService customerTraceHistoryService;

	@Autowired
	private ISystemDictionaryItemService systemDictionaryItemService;
	
	@RequestMapping("list")
	public String list(Model model, @ModelAttribute("qo")CustomerTraceHistoryQueryObject qo){
		//int i = 1/0;
		PageInfo<CustomerTraceHistory> pageInfo = customerTraceHistoryService.query(qo);
		model.addAttribute("pageInfo",pageInfo);

		//交流方式
		List<SystemDictionaryItem> traceTypes = systemDictionaryItemService.selectBySystemDictionarySn("traceType");
		model.addAttribute("traceTypes", traceTypes);
		return "customerTraceHistory/list";
	}
	
	/*@RequestMapping("input")
	@RequriedPermission(name="部门编辑",expression = "customerTraceHistory:input")
	public String input(Long id,Model model){
		if (id!=null) {
			CustomerTraceHistory customerTraceHistory = customerTraceHistoryService.select(id);
			model.addAttribute("customerTraceHistory", customerTraceHistory);
		}
		return "customerTraceHistory/input";
	}*/
	

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(CustomerTraceHistory customerTraceHistory){
		try {
			if (customerTraceHistory.getId()!=null) {
				customerTraceHistoryService.update(customerTraceHistory);
			}else {
				customerTraceHistoryService.insert(customerTraceHistory);
			}
			return new JsonResult(true,"新增或修改成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"新增或修改失败");
		}

	}
}
